package com.hrzyit.whistle.user.ui.activity

import android.os.Bundle
import com.hrzyit.base.ext.enable
import com.hrzyit.base.ext.onClick
import com.hrzyit.base.ui.activity.BaseMvpActivity
import com.hrzyit.whistle.user.R
import com.hrzyit.whistle.user.data.bean.UserInfo
import com.hrzyit.whistle.user.injection.component.DaggerUserComponent
import com.hrzyit.whistle.user.injection.module.UserModule
import com.hrzyit.whistle.user.presenter.ResetPwdPresenter
import com.hrzyit.whistle.user.presenter.view.ResetPwdView
import kotlinx.android.synthetic.main.activity_reset_pwd.*
import org.jetbrains.anko.clearTop
import org.jetbrains.anko.intentFor
import org.jetbrains.anko.singleTop
import org.jetbrains.anko.toast

/**
 * 用户重置密码界面
 */
class ResetPwdActivity : BaseMvpActivity<ResetPwdPresenter>(), ResetPwdView {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_reset_pwd)

        initView()
    }

    /**
     * 初始化视图
     */
    private fun initView() {

        with(mConfirmBtn) {
            enable(mPwdConfirmEt) { isBtnEnable() }
            enable(mPwdEt) { isBtnEnable() }
        }

        // 注册重置密码按钮点击事件
        mConfirmBtn.onClick {
            if (mPwdEt.text.toString() != mPwdConfirmEt.text.toString()){
                toast("密码不一致")
                return@onClick
            }
            mPresenter.resetPwd(intent.getStringExtra("mobile"),mPwdEt.text.toString())
        }

    }

    /**
     * 判断按钮是否可用
     */
    private fun isBtnEnable(): Boolean {

        return mPwdConfirmEt.text.isNullOrBlank().not() &&
                mPwdEt.text.isNullOrBlank().not()
    }

    /**
     * Dagger注册
     */
    override fun injectComponent() {

        DaggerUserComponent.builder()
            .activityComponent(mActivityComponent)
            .userModule(UserModule())
            .build().inject(this)
        mPresenter.mView = this

    }

    /**
     * 成功回调：重置密码
     */
    override fun onResetPwdResult(result: UserInfo) {
        // 重置密码后，跳转到登录页面
        startActivity(intentFor<LoginActivity>().singleTop().clearTop())
    }

}
